#include <stdio.h>
#include <stdbool.h>
#include <string.h>

bool checkPAT(char* str){
	int a = 0, b = 0, c = 0, number_t = 0, number_p = 0;
	for(int i = 0; i < strlen(str); i++){
		if(str[i] != 'A' && str[i] != 'P' && str[i] != 'T'){
			return false;
		}
		if(str[i] == 'P'){
			number_p++;
			a = i;
		}
		if(str[i] == 'T'){
			number_t++;
			b = i - a - 1;
			c = strlen(str) - 1 - i;
		}
	}
	if(a*b == c && number_t == 1&& number_p == 1 && b != 0){
		return true;
	}
	return false;
}

int main(){
	int number = 0;
	int i = 0;
	char str[10][105]={0};

	scanf("%d", &number);
	for(; i < number; i++){
		scanf("%s", &str[i]);
	}

	for(i = 0;i < number; i++){
		printf("%s\n", checkPAT(str[i])?"YES":"NO");
	}
	return 0;
}
